package view;

import constants.Constants;
import controllers.CustomerController;
import controllers.InfoBookController;
import controllers.PaymentController;
import controllers.RoomController;
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.AbstractTableModel;

import view.book.BookingPanel;
import view.room.RoomPanel;
public class MainView extends JFrame{
	private static final long serialVersionUID = 1L;
	RoomPanel jpRoom;
	public MainView(){
		super("QUẢN LÍ KHÁCH SẠN");
		setMinimumSize(new Dimension(800, 400));
		setSize(1000, 600);
		setLocationRelativeTo(null);
//		setExtendedState(JFrame.MAXIMIZED_BOTH);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
		final JTabbedPane tabView = new JTabbedPane(JTabbedPane.LEFT);
		tabView.addTab("LỄ TÂN", null);	
		tabView.addTab("PHÒNG", jpRoom = new RoomPanel(RoomController.getAllType()));
		tabView.addTab("ĐẶT PHÒNG", new view.book.BookingPanel (tableModel()[0]));
		tabView.addTab("THUÊ PHÒNG",new view.book.CheckInPanel(tableModel()[1]));
		tabView.addTab("TRẢ PHÒNG", new view.book.CheckOutPanel(tableModel()[2]));
                tabView.addTab("KHÁCH HÀNG", new view.customer.CustomerPanel(tableModel()[3]));
                tabView.addTab("HÓA ĐƠN", new view.statistic.PaymentPanel(tableModel()[4]));                
		tabView.setEnabledAt(0, false);
		tabView.setSelectedIndex(1);		
		String[] tabNames = {"LỄ TÂN", "PHÒNG", "ĐẶT PHÒNG","THUÊ PHÒNG","TRẢ PHÒNG","KHÁCH HÀNG","HÓA ĐƠN"};
               
		for (int i=0; i<tabNames.length; i++){
			tabView.setTabComponentAt(i, new CustomTab(tabNames[i]));
		}
		
		add(new ToolBar(), BorderLayout.NORTH);
		add(new StatusBar(), BorderLayout.SOUTH);
		add(tabView, BorderLayout.CENTER);
                
                tabView.addChangeListener(new ChangeListener() {
                    public void stateChanged(ChangeEvent e) {
                        System.out.println("Tab=" + tabView.getSelectedIndex());
                        switch(tabView.getSelectedIndex()){
                                case 1:
                                     tabView.setComponentAt(1,jpRoom = new RoomPanel(RoomController.getAllType()));
                                    break;
                                case 2:
                                    tabView.setComponentAt(2,new view.book.BookingPanel(tableModel()[0]));
                                    break;
                                case 3:
                                    tabView.setComponentAt(3,new view.book.CheckInPanel(tableModel()[1]));
                                    break;
                                case 4:
                                    tabView.setComponentAt(4,new view.book.CheckOutPanel(tableModel()[2]));
                                    break;
                                case 5:
                                    tabView.setComponentAt(5,new view.customer.CustomerPanel(tableModel()[3]));
                                    break;
                                case 6:
                                    tabView.setComponentAt(6,new view.statistic.PaymentPanel(tableModel()[4]));
                                    break;
                        
                        }
                    }
                });
        
                
	}
	
	public void refreshAll(){
		jpRoom.refreshAll();
	}
	private AbstractTableModel[] tableModel(){
            AbstractTableModel[] tableModel ={
                    new model.InfoBookingModel(InfoBookController.getByStatus(Constants.STATUS_BOOK)),
                    new model.InfoBookingModel(InfoBookController.getByStatus(Constants.STATUS_CHECKIN)),
                    new model.InfoBookingModel(InfoBookController.getByStatus(Constants.STATUS_CHECKOUT)),
                    new model.CustomerModel(CustomerController.getAll()),
                    new model.PaymentModel(PaymentController.getAll())
             };
            return tableModel;
        }
	class CustomTab extends JLabel{
		
		public CustomTab(String title){
			super(title);
			setPreferredSize(new Dimension(120, 30));
		}
	}
}
